package com.paireach.bw.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.paireach.bw.R;
import com.paireach.bw.bean.OrdersBean;

import java.util.List;

/**
 * Created by ${cqc} on 2017/3/8.
 * KA item :交货单
 * KA运单001，如果分给 a+b运输，b的交货单点击了异常签收，会导致a中的交货单状态也是异常签收，这里移动端改掉，服务端就不需要改状态了。
 */

public class KaAdapter extends RecyclerView.Adapter {
    private int headerType = 1;
    private int footerType = 2;

    private View headerView;
    private View footerView;

    MyViewHolder holder;
    private List<OrdersBean> list;

    public void addHeaderView(View headerView) {
        this.headerView = headerView;
    }


    public KaAdapter(List<OrdersBean> list) {
        this.list = list;
    }


    public void setHeaderView(View headerView) {
        this.headerView = headerView;
    }

    public void setFooterView(View footerView) {
        this.footerView = footerView;
    }

    public int getHeaderViewCount() {
        return (headerView == null) ? 0 : 1;
    }
    public int getFooterViewCount() {
        return (footerView == null) ? 0 : 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < getHeaderViewCount()) {
            return headerType;
        }
        if (position >= (list.size() + getHeaderViewCount())) {
            return footerType;
        }
        return super.getItemViewType(position - getHeaderViewCount());
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == headerType) {
            return new MyViewHolder(headerView);
        }

        if (viewType == footerType) {
            return new MyViewHolder(footerView);
        }
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ka, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == headerType) {
            return;
        }
        if (itemViewType == footerType) {
            return;
        }

        holder = (MyViewHolder) viewHolder;
        OrdersBean info = list.get(position - getHeaderViewCount());
        String dateTime = "";
        try {
            dateTime = info.getFRM_DLVY_DTT().substring(0, 10) + "  " + info.getFRM_DPND_CMTD_STRT_DTT().substring(11) + "--" + info.getFRM_DPND_CMTD_END_DTT().substring(11);
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.tv_order_num.setText("交货单：" + info.getSHPM_NUM());
        holder.tv_origin_name.setText("收货地：" + info.getTO_SHPG_LOC_NAME());
        holder.tv_origin_address.setText("收货地址：" + info.getTO_SHPG_ADDR());
        holder.tv_origin_time.setText("预计送达日期：" + dateTime);
        holder.tv_contacts.setText("联系人：" + info.getDRIVER_NAME());
        holder.tv_contacts_tel.setText("电话：" + info.getDRIVER_MOBILE());
    }


    @Override
    public int getItemCount() {
        return list.size() + getHeaderViewCount()+getFooterViewCount();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_order_num;
        TextView tv_origin_name;
        TextView tv_origin_address;
        TextView tv_origin_time;
        TextView tv_contacts;
        TextView tv_contacts_tel;
        TextView tv_icon;

        public MyViewHolder(View itemView) {
            super(itemView);
            if (itemView == headerView) {
                return;
            }
            if (itemView == footerView) {
                return;
            }
            tv_icon = (TextView) itemView.findViewById(R.id.tv_icon);
            tv_order_num = (TextView) itemView.findViewById(R.id.tv_order_num);
            tv_origin_name = (TextView) itemView.findViewById(R.id.tv_origin_name);
            tv_origin_address = (TextView) itemView.findViewById(R.id.tv_origin_address);
            tv_origin_time = (TextView) itemView.findViewById(R.id.tv_origin_time);
            tv_contacts = (TextView) itemView.findViewById(R.id.tv_contacts);
            tv_contacts_tel = (TextView) itemView.findViewById(R.id.tv_contacts_tel);
        }
    }
}


